package com.note.simpleadmin.module.system.service;

import com.note.simpleadmin.entity.TbEnvironment;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.exception.DataRepeatException;
import com.note.simpleadmin.module.system.dao.EnvironmentRepository;
import com.note.simpleadmin.utils.IdGenerator;
import com.note.simpleadmin.vo.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/***
 *@ClaseName:EnvironmentService
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/28 0028 10:19
 */
@Service
public class EnvironmentService {
    @Autowired
    private EnvironmentRepository environmentRepository;

    public TbEnvironment getOne(String id) {
        return environmentRepository.getOne(id);
    }

    public List<TbEnvironment> findAll(){
        return environmentRepository.findAll();
    }
    public void delete(String id) throws DataNotExistException
    {
        TbEnvironment environment=environmentRepository.getOne(id);
        if(environment==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        else
        {
            environmentRepository.delete(environment);
        }
    }
    public void add(TbEnvironment environment) throws DataRepeatException
    {
        if(environmentRepository.findByName(environment.getName())!=null)
        {
            throw new DataRepeatException("key已经存在");
        }
        else
        {
            environment.setId(IdGenerator.id());
            environmentRepository.save(environment);
        };
    }
    public void update(TbEnvironment environment) throws DataNotExistException
    {
        if(environmentRepository.getOne(environment.getId())==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        else
        {
            environmentRepository.save(environment);
        };
    }
    public PageBean findByPage(String type, String name, Integer pageNumber)
    {
        //判断订单状态是否合法

        if (pageNumber == null) {
            pageNumber = 0;
        }
        else
        {
            pageNumber=pageNumber-1;
        }
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        PageRequest pageRequest = new PageRequest(pageNumber, 20, sort);
        Specification<TbEnvironment> specification=new Specification<TbEnvironment>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Predicate toPredicate(Root<TbEnvironment> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(type)) {
                    predicates.add(criteriaBuilder.equal(root.get("type"), type));
                }
                if (StringUtils.isNotBlank(name)){
                    predicates.add(criteriaBuilder.equal(root.get("name"), name));
                }
                return  criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page<TbEnvironment> page = environmentRepository.findAll(specification,pageRequest);
        List<TbEnvironment> environments=page.getContent();
        PageBean<TbEnvironment> pageBean=new PageBean(page.getTotalPages(),page.getTotalElements(),page.isFirst(),page.isEmpty(),page.getNumber(), environments);;
        return pageBean;
    }
}
